#include "window.h"
#include "ui_window.h"
#include <QDebug>
#include <QFile>
#include <QMessageBox>

Window::Window(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Window)
{
    ui->setupUi(this);
    connect(ui->setButton, SIGNAL(clicked()), this, SLOT(setBtnDown()));

    QFile file(filePath);
    file.open(QIODevice::ReadOnly);
    QString curSys = file.readLine();

    if (curSys.isEmpty()) {
        qDebug() << "file error or permission denied";
        QMessageBox::critical(this, "Error", "file error or permission denied", QMessageBox::Ok);
        this->deleteLater();
        return;
    }

    curSys = curSys.split(' ').at(1);
    file.close();
    ui->curBootingOption->setText(curSys.simplified());
}

Window::~Window() {}

void Window::setBtnDown()
{
    if (ui->comboBox->currentText() == ui->curBootingOption->text()) {
        qDebug() << "Booting Option not changed";
        return;
    }
    QString str = "default " + ui->comboBox->currentText() + '\n' + "timeout 10";
    ui->curBootingOption->setText(ui->comboBox->currentText());
    QFile file(filePath);
    file.open(QIODevice::WriteOnly);
    qDebug() << str << "has been writen to the file";
    file.write(str.toLatin1());
    file.close();
    QMessageBox::about(this, "About", "Success!");
}